package flc.ast.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.TimeUtils;
import com.stark.teleprompter.lib.db.TaiciBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;

public class LinesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<TaiciBean> mDatas=new ArrayList<>();//数据
    private final Context mContext;

    public LinesAdapter(List<TaiciBean> mDatas, Context mContext) {
        this.mDatas = mDatas;
        this.mContext = mContext;
    }

    public LinesAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setmDatas(List<TaiciBean> mDatas) {
        this.mDatas = mDatas;
    }

    //第一步 定义接口
    public interface OnItemClickListener {
        void onClick(TaiciBean bean, int position);

        void onClickDelete(TaiciBean bean, int position);
    }

    private OnItemClickListener mListener;

    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemHolder(LayoutInflater.from(mContext).inflate(R.layout.item_lines, parent, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {

        if (holder instanceof ItemHolder) {
            ((ItemHolder) holder).tvContent.setText(mDatas.get(position).getContent());
            ((ItemHolder) holder).tvTime.setText(TimeUtils.millis2String(mDatas.get(position).getCreateTime(), new SimpleDateFormat("yyyy/MM/dd HH:mm")));

            ((ItemHolder) holder).ivDelete.setOnClickListener(v ->
                    mListener.onClickDelete(mDatas.get(position), position));
            holder.itemView.setOnClickListener(v ->
                    mListener.onClick(mDatas.get(position), position));
        } else {

        }
    }

    //多出尾部刷新的item
    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //item的holder
    static class ItemHolder extends RecyclerView.ViewHolder {
        private final TextView tvContent, tvTime;
        private final ImageView ivDelete;

        public ItemHolder(View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tvLinesItemContent);
            tvTime = itemView.findViewById(R.id.tvLinesItemTime);

            ivDelete = itemView.findViewById(R.id.ivLinesItemDelete);
        }
    }

}

